package cn.anycase.demo.test;

import org.junit.Test;

import com.baomidou.mybatisplus.enums.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * <p>
 * </p>
 *
 * @author yuxiaobin
 * @date 2022/9/8
 */
public class CodeGenerator {

    @Test
    public void generateCode() {
        String modulePackageName = "cn.anycase.demo";
        generateByTables("stanlogy", false, modulePackageName, "this_is_sample");
    }

    /**
     * @param authName              代码作者署名
     * @param serviceNameStartWithI //false: user -> UserService, 设置成true: user -> IUserService
     * @param modulePackageName     代码生成的包名(到模块), 后面会自动增加子包名.entity, .mapper, .service
     * @param tableNames            表名，多个可以 TBL_A, TBL_B, oracle是大写的表名
     */
    private void generateByTables(String authName, boolean serviceNameStartWithI, String modulePackageName, String... tableNames) {
        GlobalConfig config = new GlobalConfig();
        String dbUrl = "jdbc:mysql://172.18.10.104:3306/ChangeToYourDataBaseName?characterEncoding=utf-8&useSSL=false";
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
                .setUsername("root")
                .setPassword("123456")
                .setDriverName("com.mysql.jdbc.Driver");
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true)
                .setEntityLombokModel(true)//使用lombok模式
                .entityTableFieldAnnotationEnable(true)//强制注解
                .setDbColumnUnderline(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setRestControllerStyle(true)
                .setInclude(tableNames);//修改替换成你需要的表名，多个表名传数组
        config.setActiveRecord(false)
                .setEnableCache(false)
                .setAuthor(authName)
                .setIdType(IdType.UUID)
                .setOutputDir("d:\\codeGen")
//                .setOutputDir("Users/rhion/drbox")
                .setFileOverride(true);
        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
        }
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(
                        new PackageConfig()
                                .setParent(modulePackageName)
                                .setController("controller")
                                .setEntity("entity")
                ).execute();
    }
}
